package com.secsys.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import com.secsys.dto.StoFinanceperstockDTO;
import com.secsys.dto.StoStockDTO;
import com.secsys.model.TbStoFinanceperstock;
import com.secsys.model.TbStoStock;
import com.secsys.service.StoFinancePerStockService;
import com.secsys.service.StoStockService;
import com.secsys.util.DateFormatUtils;

public class StoFinancePerStockServiceImpl extends ServiceImpl<TbStoFinanceperstock,Integer> implements StoFinancePerStockService {

	private StoStockService stoStockService;
	
	@Override
	public List<StoFinanceperstockDTO> queryStoFinancePerStockByStockCode(String stockCode) {
		TbStoStock tbStoStock=stoStockService.findByProperty("from TbStoStock where stockCode=?", String.valueOf(stockCode)).get(0);
		return queryStoFinancePerStockByTbStoStock(tbStoStock);
	}

	@Override
	public List<StoFinanceperstockDTO> queryStoFinancePerStockByTbStoStock(TbStoStock tbStoStock) {
		String hql="from TbStoFinanceperstock as tsf where tsf.tbStoStock=?";
		return foreachGetDtoFromPojo(this.findByProperty(hql, tbStoStock)); 
	}
	
	@Override
	public List<StoFinanceperstockDTO> queryStoFinancePerStockByDate(Date fieldDate) {
		String hql="from TbStoFinanceperstock as tsf where tsf.fieldDate=?";
		return foreachGetDtoFromPojo(this.findByProperty(hql, fieldDate)); 
	}

	@Override
	public List<StoFinanceperstockDTO> queryStoFinancePerStockByTbStoStockAndDate(
			TbStoStock tbStoStock, Date fieldDate) {
		String hql="from TbStoFinanceperstock as tsf where tsf.tbStoStock=? and tss.fieldDate=?";
		return foreachGetDtoFromPojo(this.findByProperty(hql, new Object[]{tbStoStock,fieldDate})); 
	}

	@Override
	public List<StoFinanceperstockDTO> queryStoFinancePerStockByTbStoStockCodeAndDate(
			int stockCode, Date fieldDate) {
		TbStoStock tbStoStock=stoStockService.findByProperty("from TbStoStock where stockCode=?", String.valueOf(stockCode)).get(0);
		return queryStoFinancePerStockByTbStoStockAndDate(tbStoStock,fieldDate);
	}

	
	
	
	
	@Override
	public int countSearchWithNetAsset(Date startDate,Date endDate, double num, int updown) {
		String hql=null;
		if (updown==1) {
			hql="from TbStoFinanceperstock as tsf2,TbStoStock as tss,TbStoFinanceperstock as tsf1 where tss.id=tsf1.tbStoStock and " +
			" tsf1.tbStoStock=tsf2.tbStoStock and  tsf1.fieldDate=? and tsf2.fieldDate=? and tsf2.perNetAsset> tsf1.perNetAsset and " +
			" tsf2.perNetAsset/tsf1.perNetAsset >?";
		}else {
			hql="from TbStoFinanceperstock as tsf2,TbStoStock as tss,TbStoFinanceperstock as tsp1 where tss.id=tsf1.tbStoStock and " +
			" tsf1.tbStoStock=tsf2.tbStoStock and  tsf1.fieldDate=? and tsf2.fieldDate=? and tsf2.perNetAsset< tsf1.perNetAsset and " +
			" tsf1.perNetAsset/tsf2.perNetAsset >?";
		}
		num=num/100+1;
		
		int temp=startDate.getYear();
		String startDateString=String.valueOf((1900+temp)+"-12-31");
		temp=endDate.getYear();
		String endDateString=String.valueOf((temp+1900)+"-12-31");
		
		List<Object> list=this.dao.findObject(hql, new Object[]{DateFormatUtils.convertStringToDate(startDateString),
				DateFormatUtils.convertStringToDate(endDateString),num});
		List<StoFinanceperstockDTO> stoFinanceperstockDTOList=new ArrayList<StoFinanceperstockDTO>();
		for (Object object : list) {
			StoFinanceperstockDTO stoFinanceperstockDTO=getDtoFromPojo((TbStoFinanceperstock)((Object[])object)[0]); 
			stoFinanceperstockDTOList.add(stoFinanceperstockDTO);
		}
		return list.size();
	}
	
	

	@Override
	public int countSearchWithNetIncome(Date startDate, Date endDate, double num,int updown) {
		String hql=null;
		if (updown==1) {
			hql="from TbStoFinanceperstock as tsf2,TbStoStock as tss,TbStoFinanceperstock as tsf1 where tss.id=tsf1.tbStoStock and " +
			" tsf1.tbStoStock=tsf2.tbStoStock and  tsf1.fieldDate=? and tsf2.fieldDate=? and " +
			" tsf2.perIncome/tsf2.perNetAsset > tsf1.perIncome/tsf1.perNetAsset and " +
			" (tsf2.perIncome*tsf1.perNetAsset)/(tsf2.perNetAsset*tsf1.perIncome) >? ";
		}else {
			hql="from TbStoFinanceperstock as tsf2,TbStoStock as tss,TbStoFinanceperstock as tsp1 where tss.id=tsf1.tbStoStock and " +
			" tsf1.tbStoStock=tsf2.tbStoStock and  tsf1.fieldDate=? and tsf2.fieldDate=? and " +
			" tsf2.perIncome/tsf2.perNetAsset < tsf1.perIncome/tsf1.perNetAsset" +
			" (tsf1.perIncome*tsf2.perNetAsset)/(tsf1.perNetAsset*tsf2.perIncome) >?";
		}
		num=num/100+1;
		
		int temp=startDate.getYear();
		String startDateString=String.valueOf((1900+temp)+"-12-31");
		temp=endDate.getYear();
		String endDateString=String.valueOf((temp+1900)+"-12-31");
		
		List<Object> list=this.dao.findObject(hql, new Object[]{DateFormatUtils.convertStringToDate(startDateString),
				DateFormatUtils.convertStringToDate(endDateString),num});
		List<StoFinanceperstockDTO> stoFinanceperstockDTOList=new ArrayList<StoFinanceperstockDTO>();
		for (Object object : list) {
			StoFinanceperstockDTO stoFinanceperstockDTO=getDtoFromPojo((TbStoFinanceperstock)((Object[])object)[0]); 
			stoFinanceperstockDTOList.add(stoFinanceperstockDTO);
		}
		return list.size();
	}

	@Override
	public HashMap<String, List> querySearchWithNetIncome(Date startDate,	Date endDate, double num, int updown, int offset, int count) {
		String hql=null;
		if (updown==1) {
			hql="from TbStoFinanceperstock as tsf2,TbStoStock as tss,TbStoFinanceperstock as tsf1 where tss.id=tsf1.tbStoStock and " +
			" tsf1.tbStoStock=tsf2.tbStoStock and  tsf1.fieldDate=? and tsf2.fieldDate=? and " +
			" tsf2.perIncome/tsf2.perNetAsset > tsf1.perIncome/tsf1.perNetAsset and " +
			" (tsf2.perIncome*tsf1.perNetAsset)/(tsf2.perNetAsset*tsf1.perIncome) >? " +
			" order by (tsf2.perIncome*tsf1.perNetAsset)/(tsf2.perNetAsset*tsf1.perIncome) desc";
		}else {
			hql="from TbStoFinanceperstock as tsf2,TbStoStock as tss,TbStoFinanceperstock as tsp1 where tss.id=tsf1.tbStoStock and " +
			" tsf1.tbStoStock=tsf2.tbStoStock and  tsf1.fieldDate=? and tsf2.fieldDate=? and " +
			" tsf2.perIncome/tsf2.perNetAsset < tsf1.perIncome/tsf1.perNetAsset" +
			" (tsf1.perIncome*tsf2.perNetAsset)/(tsf1.perNetAsset*tsf2.perIncome) >?" +
			" order by (tsf1.perIncome*tsf2.perNetAsset)/(tsf1.perNetAsset*tsf2.perIncome) desc";
		}
		num=num/100+1;
		
		int temp=startDate.getYear();
		String startDateString=String.valueOf((1900+temp)+"-12-31");
		temp=endDate.getYear();
		String endDateString=String.valueOf((temp+1900)+"-12-31");
		
		List<Object> list=this.dao.findObject(hql, new Object[]{DateFormatUtils.convertStringToDate(startDateString),
				DateFormatUtils.convertStringToDate(endDateString),num},offset,count);
		
		List<StoFinanceperstockDTO> stoFinanceperstockDTOList=new ArrayList<StoFinanceperstockDTO>();
		List<StoFinanceperstockDTO> oldYearStoFinanceperstockDTOList=new ArrayList<StoFinanceperstockDTO>();
		List<StoStockDTO> stockDTOList=new ArrayList<StoStockDTO>();
		for (Object object : list) {
			StoFinanceperstockDTO stoFinanceperstockDTO=getDtoFromPojo((TbStoFinanceperstock)((Object[])object)[0]); 
			stoFinanceperstockDTOList.add(stoFinanceperstockDTO);
			
			StoStockDTO stockDTO=stoStockService.getDtoFromPojo((TbStoStock)((Object[])object)[1]);
			stockDTOList.add(stockDTO);
			
			StoFinanceperstockDTO oldStoFinanceperstockDTO=getDtoFromPojo((TbStoFinanceperstock)((Object[])object)[0]); 
			oldYearStoFinanceperstockDTOList.add(oldStoFinanceperstockDTO);
		}
		HashMap<String, List> map=new HashMap<String, List>(); 
		map.put("stoFinanceperstockDTOList", stoFinanceperstockDTOList);
		map.put("stockDTOList", stockDTOList); 
		map.put("oldYearStoFinanceperstockDTOList", oldYearStoFinanceperstockDTOList); 
		return map; 
	}

	@Override
	public HashMap<String, List> querySearchWithNetAsset(Date startDate,Date endDate, double num, int updown,int offset, int count) {
		String hql=null;
		if (updown==1) {
			hql="from TbStoFinanceperstock as tsf2,TbStoStock as tss,TbStoFinanceperstock as tsf1 where tss.id=tsf1.tbStoStock and " +
			" tsf1.tbStoStock=tsf2.tbStoStock and  tsf1.fieldDate=? and tsf2.fieldDate=? and tsf2.perNetAsset> tsf1.perNetAsset and " +
			" tsf2.perNetAsset/tsf1.perNetAsset >? order by tsf2.perNetAsset/tsf1.perNetAsset desc";
		}else {
			hql="from TbStoFinanceperstock as tsf2,TbStoStock as tss,TbStoFinanceperstock as tsp1 where tss.id=tsf1.tbStoStock and " +
			" tsf1.tbStoStock=tsf2.tbStoStock and  tsf1.fieldDate=? and tsf2.fieldDate=? and tsf2.perNetAsset< tsf1.perNetAsset and " +
			" tsf1.perNetAsset/tsf2.perNetAsset >? order by tsf1.perNetAsset/tsf2.perNetAsset desc";
		}
		num=num/100+1;

		int temp=startDate.getYear();
		String startDateString=String.valueOf((1900+temp)+"-12-31");
		temp=endDate.getYear();
		String endDateString=String.valueOf((temp+1900)+"-12-31");
		
		List<Object> list=this.dao.findObject(hql, new Object[]{DateFormatUtils.convertStringToDate(startDateString),
				DateFormatUtils.convertStringToDate(endDateString),num},offset,count);
		
		List<StoFinanceperstockDTO> stoFinanceperstockDTOList=new ArrayList<StoFinanceperstockDTO>();
		List<StoFinanceperstockDTO> oldYearStoFinanceperstockDTOList=new ArrayList<StoFinanceperstockDTO>();
		List<StoStockDTO> stockDTOList=new ArrayList<StoStockDTO>();
		for (Object object : list) {
			StoFinanceperstockDTO stoFinanceperstockDTO=getDtoFromPojo((TbStoFinanceperstock)((Object[])object)[0]); 
			stoFinanceperstockDTOList.add(stoFinanceperstockDTO);
			StoStockDTO stockDTO=stoStockService.getDtoFromPojo((TbStoStock)((Object[])object)[1]);
			stockDTOList.add(stockDTO);
			StoFinanceperstockDTO oldYearStoFinanceperstockDTO=getDtoFromPojo((TbStoFinanceperstock)((Object[])object)[2]); 
			oldYearStoFinanceperstockDTOList.add(oldYearStoFinanceperstockDTO);
		}
		HashMap<String, List> map=new HashMap<String, List>(); 
		map.put("stoFinanceperstockDTOList", stoFinanceperstockDTOList);
		map.put("stockDTOList", stockDTOList); 
		map.put("oldYearStoFinanceperstockDTOList", oldYearStoFinanceperstockDTOList); 
		
		return map; 
	}

	/**
	 * 批量地将持久化对象POJO的List<TbStoFinanceperstock>转换成领域对象DTO的List<StoFinanceperstockDTO>
	 * @param 	List<TbStoFinanceperstock>
	 * 			持久化对象POJO的List<TbStoFinanceperstock>
	 * @return	List<StoFinanceperstockDTO>
	 * 			领域对象DTO的List<StoFinanceperstockDTO>
	 */
	private List<StoFinanceperstockDTO> foreachGetDtoFromPojo(List<TbStoFinanceperstock> tbStoFinanceperstockList){
		List<StoFinanceperstockDTO> stoFinanceperstockDTOList=new ArrayList<StoFinanceperstockDTO>();
		for (TbStoFinanceperstock tbStoFinanceperstock : tbStoFinanceperstockList) {
			stoFinanceperstockDTOList.add(getDtoFromPojo(tbStoFinanceperstock));
		}
		return stoFinanceperstockDTOList;
	}
	
	/**
	 * 将持久化对象POJO的TbStoFinanceperstock转换成领域对象DTO的StoFinanceperstockDTO
	 * @param 	TbStoFinanceperstock
	 * 			持久化对象POJO的TbStoFinanceperstock
	 * @return	StoFinanceperstockDTO
	 * 			领域对象DTO的StoFinanceperstockDTO
	 */
	private StoFinanceperstockDTO getDtoFromPojo(TbStoFinanceperstock tbStoFinanceperstock){
		StoFinanceperstockDTO stoFinanceperstockDTO=new StoFinanceperstockDTO(
				tbStoFinanceperstock.getId(),tbStoFinanceperstock.getFieldDate(),
				tbStoFinanceperstock.getPerIncome(),tbStoFinanceperstock.getPerIncomeDeduct(),
				tbStoFinanceperstock.getPerNetAsset(),tbStoFinanceperstock.getAccumulation(),
				tbStoFinanceperstock.getUncutProfit(),tbStoFinanceperstock.getPerManageCash(),
				tbStoFinanceperstock.getPerCash()
		);
		return stoFinanceperstockDTO;
	}
	
	public StoStockService getStoStockService() {
		return stoStockService;
	}

	public void setStoStockService(StoStockService stoStockService) {
		this.stoStockService = stoStockService;
	}
	
	
}
